using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
    #region using
    using System.Web.Mvc;
    using TemplateWeb.Models.ViewModels;
    using TemplateWeb.Models;
    using TemplateWeb.Models.Repositories;
    using TemplateWeb.Helpers;
    using System.Web.Security;
    using System.Globalization;
    #endregion
    public partial class AdminController : BaseController
    {
        public ActionResult SelectSach()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_SACH
            };
            return View(CreateViewModel(data));
        }


        public ActionResult SelectSachByIDLoaiSach(int idloaisach)
        {
            var data = new DataTransferViewModel
            {
                IDLoaiSach = idloaisach,
                EnumViewModelType = EnumViewModel.ADMIN_SACH,
            };
            return View("SelectSach", CreateViewModel(data));
        }

        public ActionResult SelectSachByIDLoaiSachPaging(int idloaisach, int page = 1)
        {
            var data = new DataTransferViewModel
            {
                IDLoaiSach = idloaisach,
                EnumViewModelType = EnumViewModel.ADMIN_SACH,
                CurrentPage = page
            };
            return PartialView("Templates/TH_ListSach", CreateViewModel(data));
        }


        [AcceptVerbs(HttpVerbs.Delete)]
        public JsonResult DeleteSach(int id)
        {
            // Delete sach truoc
            var result = _repSach.Delete(id);
            if (result)
            {
                var spItem = _repSanPham.SelectByID(id);
                var fileName = spItem.HinhMinhHoa;
                result = _repSanPham.Delete(id);
                if (result)
                {
                    if (fileName != "noimages.jpg")
                    {
                        var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
                        DeleteFile(fullPath);
                    }
                }
            }

            return Json(new
            {
                Success = result,
                RecordCount = _repSach.SelectAll().Count,
                DeleteId = id
            });
        }

        public ActionResult DetailOfSach(int id)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_SACH,
                IntID = id
            };
            return View(CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateSach(FormCollection forms)
        {
            string refName = string.Empty;
            long gia;
            int sotrang;
            int soluongton;

            string id = forms.GetValues("Sach_ID")[0];
            string idloaisach = forms.GetValues("Sach_IDLoaiSach")[0];
            string tensanpham = forms.GetValues("Sach_TenSanPham")[0];
            string noidung = forms.GetValues("Sach_NoiDung")[0];
            string noidungrutgon = StringHelper.Truncate(forms.GetValues("Sach_NoiDungRutGon")[0], WebConfiguration.SachSortDescription);
            string tacgia = forms.GetValues("Sach_TacGia")[0];
            string nhaxuatban = forms.GetValues("Sach_NhaXuatBan")[0];
            string kho = forms.GetValues("Sach_Kho")[0];
            string hangmuc = forms.GetValues("Sach_HangMuc")[0];
            string ngayxuatban = forms.GetValues("Sach_NgayXuatBan")[0];
            var file = Request.Files["Sach_HinhMinhHoa"];

            // Update Sach
            var sachItem = _repSach.SelectByID(int.Parse(id));

            if (int.TryParse(forms.GetValues("Sach_SoTrang")[0].Replace(",", string.Empty).Replace(".", string.Empty), out sotrang))
            {
                sachItem.SoTrang = sotrang;
            }

            sachItem.IDLoaiSach = int.Parse(idloaisach.Replace(".", string.Empty).Replace(",", string.Empty));
            sachItem.NoiDung = noidung;
            sachItem.NoiDungRutGon = noidungrutgon;
            sachItem.TacGia = tacgia;
            sachItem.NhaXuatBan = nhaxuatban;
            sachItem.Kho = kho;
            sachItem.HangMuc = hangmuc;

            if (!string.Empty.Equals(ngayxuatban))
            {
                sachItem.NgayXuatBan = DateTime.Parse(ngayxuatban, new CultureInfo("en"));
            }

            var result = _repSach.Save();

            // Update tiep san pham
            if (result)
            {
                var spItem = _repSanPham.SelectByID(int.Parse(id));

                if (!int.TryParse(forms.GetValues("Sach_SoLuongTon")[0].Replace(",", string.Empty).Replace(".", string.Empty), out soluongton))
                {
                    spItem.SoLuongTon = soluongton;
                }

                if (!long.TryParse(forms.GetValues("Sach_Gia")[0].Replace(",", string.Empty).Replace(".", string.Empty), out gia))
                {
                    spItem.GiaBan = gia;
                }

                spItem.TenSanPham = tensanpham;

                if (file.ContentLength != 0)
                {
                    result = UploadFile(file, "/Content/Images/Items/", ref refName);
                    spItem.HinhMinhHoa = refName;
                }

                result = _repSanPham.Save();
            }

            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_SACH,
                IntID = int.Parse(id.Replace(".", string.Empty).Replace(",", string.Empty)),
                InfoText = result ? "Item has been updated" : "Cannot update this item"
            };
            return View("DetailOfSach", CreateViewModel(data));
        }


        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertSach(FormCollection forms)
        {
            long gia;
            int sotrang;
            int soluongton;
            string refName = string.Empty;
            var result = false;

            string idloaisach = forms.GetValues("Sach_IDLoaiSach")[0];
            string tensanpham = forms.GetValues("Sach_TenSanPham")[0];
            string noidung = forms.GetValues("Sach_NoiDung")[0];
            string noidungrutgon = StringHelper.Truncate(forms.GetValues("Sach_NoiDungRutGon")[0], WebConfiguration.SachSortDescription);
            string tacgia = forms.GetValues("Sach_TacGia")[0];
            string nhaxuatban = forms.GetValues("Sach_NhaXuatBan")[0];
            string kho = forms.GetValues("Sach_Kho")[0];
            string hangmuc = forms.GetValues("Sach_HangMuc")[0];
            string ngayxuatban = forms.GetValues("Sach_NgayXuatBan")[0];
            var file = Request.Files["Sach_HinhMinhHoa"];

            // Insert san pham truoc
            var spItem = new SanPham();

            if (int.TryParse(forms.GetValues("Sach_SoLuongTon")[0].Replace(",", string.Empty).Replace(".", string.Empty), out soluongton))
            {
                spItem.SoLuongTon = soluongton;
            }

            if (long.TryParse(forms.GetValues("Sach_Gia")[0].Replace(",", string.Empty).Replace(".", string.Empty), out gia))
            {
                spItem.GiaBan = gia;
            }

            spItem.LoaiSanPham = StringEnum.GetStringValue(LoaiSanPham.SACH);
            spItem.TenSanPham = tensanpham;

            if (file.ContentLength != 0)
            {
                if (UploadFile(file, "/Content/Images/Items/", ref refName))
                {
                    spItem.HinhMinhHoa = refName;
                }
            }

            result = _repSanPham.Insert(spItem);

            // Neu insert san pham thanh cong, thi moi insert tiep sach
            if (result)
            {
                // Insert Sach
                var sachItem = new Sach
                {
                    ID = spItem.ID,
                    IDLoaiSach = int.Parse(idloaisach),
                    NoiDung = noidung,
                    NoiDungRutGon = noidungrutgon,
                    TacGia = tacgia,
                    NhaXuatBan = nhaxuatban,
                    Kho = kho,
                    HangMuc = hangmuc
                };

                if (int.TryParse(forms.GetValues("Sach_SoTrang")[0].Replace(",", string.Empty).Replace(".", string.Empty), out sotrang))
                {
                    sachItem.SoTrang = sotrang;
                }

                if (!string.Empty.Equals(ngayxuatban))
                {
                    sachItem.NgayXuatBan = DateTime.Parse(ngayxuatban, new CultureInfo("en"));
                }
                result = _repSach.Insert(sachItem);
            }

            var data = new DataTransferViewModel
            {
                IDLoaiSach = int.Parse(idloaisach),
                EnumViewModelType = EnumViewModel.ADMIN_SACH,
                InfoText = result ? "New item has been added" : "Cannot insert new item"
            };
            return View("SelectSach", CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TimKiemSach(FormCollection forms)
        {
            var tensanpham = forms.GetValues("Sach_TimKiemTenSanPham")[0];
            var loaisanpham = forms.GetValues("Sach_TimKiemLoaiSanPham")[0];

            ViewData["TuKhoa"] = tensanpham;

            var data = new DataTransferViewModel
            {
                TimKiem_TuKhoa = tensanpham,
                IDLoaiSach = int.Parse(loaisanpham),
                EnumViewModelType = EnumViewModel.ADMIN_TIMKIEM_SACH,
            };
            return View("SelectSach", CreateViewModel(data));
        }
    }
}